// 引入格式化时间函数
import formatTime from '../../utils/formatTime.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
	  detailObj: {}, // 详细消息
	  commentDetailList: [], // 评论列表
	  commentId: '', // 消息id
	  isContent: true, // 控制是否有评论对应的布局
	  userInfo: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
	  this.setData({ // 拿到对应的id
		commentId: options.commentId
	  })
	  // console.log(this.data.commentId)
	  this._loadCommentDetail() // 调用获取detail的函数
	  this.setData({
		  userInfo: wx.getStorageSync('USERINFO').userInfo
	  })
	  // console.log(this.data.userInfo, 1)
  },
  _loadCommentDetail () { // 获取详细事件
	  let commentId = this.data.commentId
	  //console.log(commentId)
	  wx.showLoading({
	  	title: '加载中...',
	  	mask: true
	  })
	  wx.cloud.callFunction({
		  name: 'shop-comment',
		  data: {
			 commentId: commentId, // 将ID传给云函数
			 $url: 'detail'
		  }
	  }).then(res => {
		  // console.log(res)
		  let commentDetailList = res.result.commentDetailList.data
		  // console.log(commentDetailList)
		  for(let i=0,len=commentDetailList.length;i<len;i++) { // 循环得到createTime 然后利用函数格式化时间
			  commentDetailList[i].createTime = formatTime(new Date(commentDetailList[i].createTime))
		  }
		  let detailObj = res.result.detail[0]
		  // console.log(detailObj)
		  this.setData({ // 得到对应数据
			  detailObj: detailObj,
			  commentDetailList: commentDetailList
		  })
		  // console.log(this.data.commentDetailList)
		  let bool = this.data.commentDetailList
		  if (bool.length == 0) { // 没有评论和有评论对应的布局
			  this.setData({
				  isContent: false
			  })
			  // console.log(bool.length, this.data.isContent)
		  } else {
			  this.setData({
				  isContent: true
			  })
		  }
		  wx.hideLoading()
	  })
  },
  onShareAppMessage() { // detail页的分享
  	const detailObj = this.data.detailObj
	// console.log(detailObj)
	const commentId = this.data.detailObj._id
	// console.log(commentId)
  	return {
  		title: detailObj.content,
  		path: `/pages/commentdetail/commentdetail?commentId=${commentId}`
  	}
  }
})